<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="昵称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="真实姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入真实姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small"  @keyup.enter.native="handleQuery">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:zsyyUser:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="zsyyUserList" border size="mini" stripe @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" v-if="false"/>
      <el-table-column label="头像地址" align="center" prop="avatarUrl" >
        <template slot-scope="scope">
          <img :src="scope.row.avatarUrl" style="width: 50px;height: 50px" @click="showImg(scope.row.avatarUrl)">
        </template>
      </el-table-column>
      <el-table-column label="昵称" align="center" prop="nickname" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="真实姓名" align="center" prop="name" />
      <el-table-column label="用户余额" align="center" prop="balance" />
      <el-table-column label="生日" align="center" prop="birthday" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" prop="gender" :formatter="genderFormat" />
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value= 1
            :inactive-value= 0
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="自动扣费状态" align="center" prop="autoPayStatus" :formatter="autoPayStatusFormat" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--  查看头像  -->
    <el-dialog :title="imgTitle" :visible.sync="imgOpen" v-if="imgOpen" width="50%" append-to-body>
      <show-imgs :imgs="imgUrl"></show-imgs> 
    </el-dialog>

  </div>
</template>

<script>
import { listZsyyUser, getZsyyUser, delZsyyUser, addZsyyUser, updateZsyyUser, exportZsyyUser,changeStatus } from "@/api/system/zsyyUser";
import ShowImgs from "@/components/ShowImgs";

export default {
  name: "ZsyyUser",
  components: {
    ShowImgs
  },
  data() {
    return {
	  //按钮loading
	  buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户信息管理表格数据
      zsyyUserList: [],
      // 弹出层标题
      title: "",
      imgOpen: false,
      imgTitle: "",
      imgUrl: "",
      // 是否显示弹出层
      open: false,
      // 0.普通会员 1.VIP会员,2.商家字典
      levelOptions: [],
      // 性别字典
      genderOptions: [],
      // 状态：0禁用 1激活字典
      statusOptions: [],
      // 自动扣费状态：1未开启，2已开启字典
      autoPayStatusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickname: undefined,
        phone: undefined,
        name: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_user_level").then(response => {
      this.levelOptions = response.data;
    });
    this.getDicts("sys_user_sex").then(response => {
      this.genderOptions = response.data;
    });
    this.getDicts("sys_user_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("user_auto_pay_status").then(response => {
      this.autoPayStatusOptions = response.data;
    });
  },
  methods: {
    /** 查询用户信息管理列表 */
    getList() {
      this.loading = true;
      listZsyyUser(this.queryParams).then(response => {
        this.zsyyUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //显示头像
    showImg(imgUrl) {
      this.imgOpen = true;
      this.imgTitle = "头像";
      this.imgUrl = imgUrl;
    },
    // 0.普通会员 1.VIP会员,2.商家字典翻译
    levelFormat(row, column) {
      return this.selectDictLabel(this.levelOptions, row.level);
    },
    // 性别字典翻译
    genderFormat(row, column) {
      return this.selectDictLabel(this.genderOptions, row.gender);
    },
    // 状态：0禁用 1激活字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 自动扣费状态：1未开启，2已开启字典翻译
    autoPayStatusFormat(row, column) {
      return this.selectDictLabel(this.autoPayStatusOptions, row.autoPayStatus);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.imgOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        nickname: undefined,
        phone: undefined,
        password: undefined,
        name: undefined,
        balance: undefined,
        loginType: undefined,
        openId: undefined,
        avatarUrl: undefined,
        level: undefined,
        birthday: undefined,
        gender: 0,
        gmtLastLogin: undefined,
        status: undefined,
        autoPayStatus: undefined,
        autoPayUpdatetime: undefined,
        sessionKey: undefined,
        createtime: undefined,
        updatetime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户信息管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getZsyyUser(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改用户信息管理";
      });
    },
    // 状态修改
    handleStatusChange(row) {
      this.$confirm('确认要修改用户【"' + row.nickname + '"】的状态吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return changeStatus(row.id, row.status);
      }).then(() => {
        this.msgSuccess("操作成功");
      }).catch(function() {
        row.status = row.status == 0 ? 1 : 0;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateZsyyUser(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addZsyyUser(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除用户信息管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          return delZsyyUser(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户信息管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportZsyyUser(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
